package fiuba.webapp.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import fiuba.model.LabelValue;
import fiuba.model.Menu;
import fiuba.model.MenuItem;
import fiuba.model.Seccion;
import fiuba.service.GenericManager;
import fiuba.service.MenuExistsException;
import fiuba.service.MenuItemManager;
import fiuba.service.MenuManager;

public class MenuItemAction extends BaseAction {
    private static final long serialVersionUID = 798032320394828L;
	private MenuItemManager menuItemManager;
	private GenericManager<Seccion, Long> seccionManager;
	private String seccionId;
    private List menuItems;
    private String id;
    private MenuItem menuItem;
    //private List <LabelValue> secciones; 
    
	public MenuItemManager getMenuItemManager() {
		return menuItemManager;
	}
	public void setMenuItemManager(MenuItemManager menuItemManager) {
		this.menuItemManager = menuItemManager;
	}
	public List getMenuItems() {
		return menuItems;
	}
	public void setMenuItems(List menuItems) {
		this.menuItems = menuItems;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public MenuItem getMenuItem() {
		return menuItem;
	}
	public void setMenuItem(MenuItem menuItem) {
		this.menuItem = menuItem;
	}
	
	public String delete() {
        menuItemManager.removeMenuItem(menuItem.getId().toString());
        saveMessage(getText("menuItem.deleted"));

	    return SUCCESS;
	}
	/**
	 * Grab the menuItem from the database based on the "id" passed in.
	 * @return success if user found
	 * @throws IOException can happen when sending a "forbidden" from response.sendError()
	 */
	public String edit() {
	    if (id != null) {
	        menuItem = menuItemManager.getMenuItem(id);
	        seccionId = menuItem.getSeccion().getId().toString();
	    } else {
	        menuItem = new MenuItem();
	        seccionId = "";
	    }      
	    
	//    List <Seccion> secciones = seccionManager.getAll();
	     
	    return SUCCESS;
	}
	
	
    public String list() {    	
        menuItems = menuItemManager.getMenuItems();
        return SUCCESS;
    }
	public GenericManager<Seccion, Long> getSeccionManager() {
		return seccionManager;
	}
	public void setSeccionManager(GenericManager<Seccion, Long> seccionManager) {
		this.seccionManager = seccionManager;
	}
	public Map<String,String> getSecciones() {

			Map<String,String> ret = new LinkedHashMap<String,String>();
			 List <Seccion> secciones = seccionManager.getAll();
			    
		     for (Seccion s : secciones){
			   	ret.put(s.getId().toString(),s.getNombre());
		     }
			return ret;
	}

    /**
     * Save menuItem
     * @return success if everything worked, otherwise input
     * @throws IOException when setting "access denied" fails on response
     */
    public String save() throws IOException {

	if (cancel != null) {
        return "cancel";
    }

    if (delete != null) {
        return delete();
    }
    
    boolean isNew = (menuItem.getId() == null);
    
    log.info("save" + isNew + menuItem);
    
    log.info("seccion" + seccionId);
    
    Seccion seccion = seccionManager.get(Long.valueOf(seccionId));
    menuItem.setSeccion(seccion);
    try {
    	menuItemManager.saveMenuItem(menuItem);	
    }catch(MenuExistsException e){
    	return ERROR;
    }
    String key = (isNew) ? "menuItem.added" : "menuItem.updated";
    saveMessage(getText(key));
    
    if (!isNew) {
        return INPUT;
    } else {
        return SUCCESS;
    }
    
}
	public String getSeccionId() {
		return seccionId;
	}
	public void setSeccionId(String seccionId) {
		this.seccionId = seccionId;
	}
    
	
}
